﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using MapInfo.MiPro.Interop;
using CustomMapinfoWindows.ViewModels;


namespace CustomMapinfoWindows
{
    /// <summary>
    /// Interaction logic for MVVMFileExplorer.xaml
    /// </summary>
    public partial class MVVMFileExplorer : UserControl
    {
        public MVVMFileExplorer()
        {
            InitializeComponent();

            string[] drives = Directory.GetLogicalDrives();
            ComputerViewModel viewmodel = new ComputerViewModel(drives);
            this.DataContext = viewmodel;
        }



        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (Globals.TreeSelectedItem != null && (Globals.TreeSelectedItem is TabFileViewModel || Globals.TreeSelectedItem is WorkSpaceViewModel))
            {
                MapinfoFileViewModel file = (MapinfoFileViewModel)Globals.TreeSelectedItem;
                file.OpenFile();     
            }
        }

        private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            if (item != null)
            {
                item.Focus();
                e.Handled = true;
            }
        }

        private void TreeView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F2)
                SetCurrentItemInEditMode(true);
        }

        private void SetCurrentItemInEditMode(bool EditMode)
        {
            // Make sure that the SelectedItem is actually a TreeViewItem
            // and not null or something else
            if (this.TreeView.SelectedItem is TabFileViewModel)
            {
                TabFileViewModel tvi = this.TreeView.SelectedItem as TabFileViewModel;

                tvi.IsEditing = true;
            }
        }
    }
}
